﻿#include "cards.h"
#include <QRandomGenerator>
Cards::Cards()
{

}

Cards::Cards(const Card &card)
{
    add(card);
}

void Cards::add(const Card &card)
{
    m_cards.insert(card);

}

void Cards::add(const Cards &cards)
{
    m_cards.unite(cards.m_cards);
}

void Cards::add(const QVector<Cards> &cards)
{
    for(int i=0; i<cards.count(); ++i)
        {
            add(cards.at(i));
        }
}

void Cards::remove(const Card &card)
{
    m_cards.remove(card);
}

void Cards::remove(const Cards &cards)
{
    m_cards.subtract(cards.m_cards);
}

void Cards::remove(QVector<Cards> cards)
{
    for(int i=0;i<cards.size();i++)
    {
        remove(cards[i]);
    }
}

Cards& Cards::operator <<(const Card &card)
{
    add(card);
    return *this;
}

Cards &Cards::operator <<(const Cards &cards)
{
    add(cards);
    return *this;
}

int Cards::cardcount()
{
    return m_cards.size();
}

bool Cards::isEmpty()
{
    return m_cards.isEmpty();
}

void Cards::clear()
{
    m_cards.clear();
}

Card::CardPoint Cards::maxPoint()
{
    Card::CardPoint max=Card::Card_Begin;
    for(auto i=m_cards.begin();i!=m_cards.end();++i)
    {
        if(max<i->point())
        {
            max=i->point();
        }
    }
    return max;
}

Card::CardPoint Cards::minPoint()
{
    Card::CardPoint min=Card::Card_End;
    for(auto it=m_cards.begin();it!=m_cards.end();++it)
    {

        if(min>it->point())
        {
            min=it->point();
        }
    }
    return min;
}

int Cards::pointCount(Card::CardPoint point)
{
    int count=0;
    for(auto i=m_cards.begin();i!=m_cards.end();++i)
    {
        if(point==i->point())
        {
            count++;
        }
    }
    return count;
}

bool Cards::contains(Card &card)
{
    return m_cards.contains(card);
}

bool Cards::contains(Cards &cards)
{
    return m_cards.contains(cards.m_cards);
}

Card Cards::takeRandCard()
{
    int num=QRandomGenerator::global()->bounded(m_cards.size());
    QSet<Card>::iterator it=m_cards.begin();
    for(int i=0;i<num;i++,it++);
    Card card=*it;
    m_cards.erase(it);
    return card;
}

QVector<Card> Cards::toCardList(Cards::SortType type)
{
    QVector<Card> list;
    for(QSet<Card>::iterator it=m_cards.begin();it!=m_cards.end();it++)
    {
        list<<*it;
    }
    if(type==Asc)
    {
        std::sort(list.begin(),list.end(),lessSort);
    }
    if(type==Desc)
    {
        std::sort(list.begin(),list.end(),greaterSort);
    }
    return list;
}
